# ======================================================================
# Shared Functions
# File: functions/read_Qualtrics.R
# ======================================================================

read_Qualtrics <- function(.file) {
  if (!file.exists(.file)) {
    stop("Error: The specified file cannot be found.")
  }

  # Turn off progress bars/messages (robust across readr/vroom versions)
  old_opts <- options(
    readr.show_progress = FALSE,
    readr.show_col_types = FALSE
  )
  on.exit(options(old_opts), add = TRUE)

  # vroom progress bar hard-off (belt-and-suspenders)
  old_env <- Sys.getenv("VROOM_SHOW_PROGRESS", unset = NA_character_)
  Sys.setenv(VROOM_SHOW_PROGRESS = "false")
  on.exit(
    {
      if (is.na(old_env)) Sys.unsetenv("VROOM_SHOW_PROGRESS") else Sys.setenv(VROOM_SHOW_PROGRESS = old_env)
    },
    add = TRUE
  )

  colnames <- names(readr::read_csv(.file, show_col_types = FALSE, n_max = 0, progress = FALSE))
  qualtrics <- readr::read_csv(.file, skip = 2, show_col_types = FALSE, progress = FALSE)
  colnames(qualtrics) <- colnames

  qualtrics
}
